package com.kool.kadmin.system.service;

import com.kool.kadmin.common.bean.ListResult;
import com.kool.kadmin.common.bean.PageListResult;
import com.kool.kadmin.common.bean.Result;
import com.kool.kadmin.common.util.PaginatorUtil;
import com.kool.kadmin.common.util.ResultUtil;
import com.kool.kadmin.system.bean.SysRoleDo;
import com.kool.kadmin.system.bean.SysUserRoleDo;
import com.kool.kadmin.system.dao.SysRoleDao;
import com.kool.kadmin.system.dao.SysUserRoleDao;
import com.kool.kadmin.system.request.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 角色管理
 *
 * @author luyu
 */
@Service
@Slf4j
public class RoleService {
    @Resource
    private SysRoleDao roleDao;
    @Resource
    private SysUserRoleDao userRoleDao;

    /**
     * 新增角色
     *
     * @param request
     * @return
     */
    public Result add(AddRoleReq request) {
        SysRoleDo roleDo = new SysRoleDo();
        roleDo.setRoleCode(request.getRoleCode())
                .setRoleName(request.getRoleName());
        roleDao.insert(roleDo);
        return ResultUtil.success();
    }

    /**
     * 修改角色
     *
     * @param request
     * @return
     */
    public Result modify(ModifyRoleReq request) {
        SysRoleDo roleDo = new SysRoleDo();
        roleDo.setId(request.getId())
                .setRoleCode(request.getRoleCode())
                .setRoleName(request.getRoleName());
        roleDao.updateById(roleDo);
        return ResultUtil.success();
    }

    /**
     * 删除角色
     *
     * @param request
     * @return
     */
    public Result delete(DeleteRoleReq request) {
        roleDao.deleteById(request.getId());
        return ResultUtil.success();
    }

    /**
     * 分页查询角色
     *
     * @param request
     * @return
     */
    public PageListResult<SysRoleDo> getPage(GetRolePageReq request) {
        int currentPage = request.getCurrentPage();
        int pageSize = request.getPageSize();
        SysRoleDo roleDo = new SysRoleDo();
        roleDo.setRoleCode(request.getRoleCode())
                .setRoleName(request.getRoleName());
        int total = roleDao.countTotal(roleDo);
        int offset = PaginatorUtil.calculateOffset(currentPage, pageSize);
        List<SysRoleDo> roleDos = roleDao.getPage(request.getRoleCode(), request.getRoleName(), offset, pageSize);
        return ResultUtil.success(roleDos, currentPage, pageSize, total);
    }

    /**
     * 绑定用户角色
     *
     * @param request
     * @return
     */
    public Result bindUserRole(BindUserRoleReq request) {
        Long userId = request.getUserId();
        List<String> roleCodes = request.getRoleCodes();
        //删除用户绑定的角色
        userRoleDao.deleteByUserId(userId);

        if (roleCodes == null || roleCodes.size() == 0) {
            return ResultUtil.success();
        }
        List<SysUserRoleDo> userRoleDos = roleCodes.stream().map(item -> {
            return new SysUserRoleDo().setUserId(userId).setRoleCode(item);
        }).collect(Collectors.toList());
        //批量插入用户角色
        userRoleDao.batchInsert(userRoleDos);
        return ResultUtil.success();
    }

    /**
     * 查询所有角色
     *
     * @return
     */
    public ListResult<SysRoleDo> getAllRoles() {
        List<SysRoleDo> roleDos = roleDao.getAllRoles();
        return ResultUtil.success(roleDos);
    }

    /**
     * 查询用户角色
     *
     * @param request
     * @return
     */
    public ListResult<SysRoleDo> getUserRoles(GetUserRolesReq request) {
        Long userId = request.getUserId();
        List<SysRoleDo> roleDos = roleDao.getUserRoles(userId);

        return ResultUtil.success(roleDos);
    }
}
